import { ContentTypeEnum } from '@/enums/httpEnum';
import { defHttp } from '@/utils/http/axios';
import {
  CreateUserParams,
  UpdateUserParams,
  UserParams,
  ChangePasswordParams,
  ChangeStatusParams,
} from './model/systemModel';

enum Api {
  CreateUser = '/user/save',
  UpdateUser = '/user/',
  DeleteUser = '/user/delete',
  UserList = '/user/list',
  setUserStatus = '/user/set/status/',
  ChangePassword = '/user/change/pass',
  AccountLogin = '/account/login',
  ChangeStatus = '/user/change/status',
}

/**
 * 创建用户
 * @params CreateUserParams
 * @returns Promise
 */
export const addUser = (params?: CreateUserParams) =>
  defHttp.post(
    {
      url: Api.CreateUser,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
      params: params,
    },
    { isTransformResponse: false },
  );

/**
 * 更新用户
 * @param id
 * @param params
 * @returns
 */
export const updateUser = (id: Number, params?: UpdateUserParams) =>
  defHttp.post(
    {
      url: `${Api.UpdateUser}${id}`,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

/**
 * 用户列表
 * @param params UserParams
 * @returns
 */
export const getUserList = (params?: UserParams) =>
  defHttp.get({ url: Api.UserList, params: params });

/**
 * 修改用户状态
 * @param id ID
 * @param status 状态
 * @returns
 */
export const setUserStatus = (id: number, status: number) =>
  defHttp.post({ url: Api.setUserStatus + id, params: { status } });

/**
 * 批量删除用户
 * @param ids
 * @returns
 */
export const deleteUserIds = (ids: string) =>
  defHttp.delete<any>(
    {
      url: Api.DeleteUser,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
      params: { ids },
    },
    { isTransformResponse: false },
  );

/**
 * 修改密码
 * @param params ChangePasswordParams
 * @returns Promise
 */
export const ChangePassword = (params?: ChangePasswordParams) =>
  defHttp.post(
    {
      url: Api.ChangePassword,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

/**
 * 登录
 * @param username
 * @param password
 * @returns Promise
 */
export const loginUser = (username: string, password: string) =>
  defHttp.post(
    {
      url: Api.AccountLogin,
      params: { username: username, password: password },
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

/**
 * 修改状态
 * @param params ChangeStatusParams
 * @returns Promise
 */
export const ChangeStatus = (id: Number, params?: ChangeStatusParams) =>
  defHttp.post(
    {
      url: `${Api.ChangeStatus}/${id}`,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );
